package com.example.ch03updown.controller;

import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

@RestController
public class UpandDownController {

    //文件上传
    @RequestMapping(value = "/upavatar", method = RequestMethod.POST)
    public String up(@RequestBody MultipartFile file, HttpServletRequest request) {
        System.out.println("进入控制器");
        //获取static/img在服务器的路径
        String realPath = request.getServletContext().getRealPath("img");
        //如果img文件夹不存在 则创建
        File file1 = new File(realPath);
        if (!file1.exists()){
            file1.mkdir();
        }
        System.err.println(realPath);
        //ResourceUtils.getURL(“classpath:”).getPath()
       /* String path1=null;
        try {
             path1 = ResourceUtils.getURL("classpath:static/img").getPath();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        System.err.println(path1);*/


        try {
            System.out.println(file.getOriginalFilename());
            file.transferTo(new File(file1, file.getOriginalFilename()));
        } catch (IOException e) {
            throw new RuntimeException();
        }
        System.out.println("上传成功");
        return "img/" + file.getOriginalFilename();
    }

    //文件下载
    @RequestMapping(value = "/downavatar")
    public String down(String file,HttpServletResponse response,HttpServletRequest request) {
        ServletOutputStream out;
        try {
            out = response.getOutputStream();
            //response.setHeader("content-type","image/png");
            //String path = ResourceUtils.getURL("classpath:static/img/").getPath();
            String img = request.getServletContext().getRealPath("img");

            //设置成浏览器无法打开的类型，让其提示另存为
            response.setContentType("application/octet-stream");
            //设置响应头 拿到另存为文件的名字
            response.setHeader("content-disposition", "attachment;filename="+file);

            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(img+file));
            //IOUtils.copy();
            System.out.println(img+file);
            byte[] bytes = new byte[1024];
            int lenth = 0;
            while ((lenth = bis.read(bytes)) != -1) {
                out.write(bytes, 0, lenth);
            }

            out.flush();
            out.close();
            bis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }


        return null;
    }

    //查询所有图片
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public List<String> list(HttpServletRequest request) {
        System.out.println("list访问成功");
        ArrayList<String> list = new ArrayList<>();
        String img = request.getServletContext().getRealPath("img");

        File file = new File(img);
        //获取当前目录下所有资源
        String[] strings = file.list();
        if (strings != null && strings.length > 0) {
            System.out.println("不为空");
            for (int i = 0; i < strings.length; i++) {
                list.add(strings[i]);
            }
        }
        return list;
    }
}
